﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using WCS_Entity.Dto;
using WCS_Entity.Dto.UserDto;
using WCS_Service.Interface;
using static ICSharpCode.SharpZipLib.Zip.ExtendedUnixData;

namespace WCS.Controllers
{
    /// <summary>
    /// 用户管理接口
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class UserController : ControllerBase
    {
        private readonly IUserService _userService;

        public UserController(IUserService userService)
        {
            _userService = userService;
        }


        #region 用户信息管理
        /// <summary>
        /// 查询指定用户信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<WebApiCallBack> QueryUserAsync(int userId)
        {
            var result = new WebApiCallBack() { status = true };
            var user = await _userService.QueryUserAsync(userId);
            if (user is null)
            {
                result.status = false;
                result.msg = "用户信息获取失败";
                result.errCode = 14007;
                return result;
            }
            result.data = user;
            return result;
        }
        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> UpdateUserAsync([FromBody] UpdateUserDto dto)
        {
            var result = new WebApiCallBack();
            var userInfo = await _userService.UpdateUserAsync(dto);
            if (userInfo is null)
            {
                result.status = false;
                result.msg = "用户信息更新失败";
                return result;
            }
            result.data = userInfo;
            return result;
        }
        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> CreateUserAsync([FromBody] CreateUserDto dto)
        {
            var result = new WebApiCallBack();
            var userInfo = await _userService.CreateUserAsync(dto);
            if (userInfo is null)
            {
                result.status = false;
                result.msg = "用户创建失败,用户名重复";
                return result;
            }
            result.msg = "用户创建成功";
            result.data = userInfo;
            return result;
        }
        /// <summary>
        /// 查询所有用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> QueryAllUserAsync()
        {
            var result = new WebApiCallBack();
            var userInfos = await _userService.QueryAllUserAsync();
            if (result is null)
            {
                result.status = false;
                result.msg = "所有用户查询失败";
                return result;
            }
            result.data = userInfos;
            return result; 
        }

        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="newPwd"></param>
        /// <param name="oldPwd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> UpdateUserPassWordAsync([FromBody]ModifyUserPasswordDto dto)
        {
            var result = new WebApiCallBack();
            if (string.IsNullOrEmpty(dto.newPwd) || string.IsNullOrEmpty(dto.oldPwd))
            {
                result.status = false;
                result.msg = "新密码或旧密码不能为空";
                return result;
            }
            if (dto.newPwd == dto.oldPwd)
            {
                result.status = false;
                result.msg = "新密码或旧密码不能相同";
                return result;
            }
            var value = await _userService.UpdatePwdAsync(dto.userId, dto.newPwd, dto.oldPwd);
            if (!value.Item1)
            {
                result.status = false;
                result.msg = value.Item2;
                return result;
            }
            result.msg = "密码修改成功";
            return result;
        }
        /// <summary>
        /// 删除指定用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<WebApiCallBack> DeleteUserAsync(int id)
        {
            var result = new WebApiCallBack();
            var flag = await _userService.DeleteUserAsync(id);
            result.status=flag;
            result.msg = flag ? "删除成功" : "删除失败";
            return result;
        }
        #endregion

        #region 登录管理
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> LoginAsync([FromBody] UserLoginDto dto)
        {
            var result = new WebApiCallBack();

            return result;
        }
        /// <summary>
        /// 登出
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> LogoutAsync()
        {
            var result = new WebApiCallBack();

            return result;
        }
        #endregion

    }
}
